package com.april.dcds;

/**
 * @author April
 * @param <E>
 *  队列的实现 入队出队 查看队列的长度
 */
public class LinkedQuene<E> {
    private LinkNode<E> front;
    private LinkNode<E> rear;
    private int size=0;
    public LinkedQuene(){
        this.front=new LinkNode<E>();
        this.rear=front;
    }
    public LinkedQuene(E e){
        this.front=new LinkNode<E>();
        this.rear=front;
        this.add(e);
    }

    @Override
    public String toString() {
        String str="LinkedQuene{";
        LinkNode node=front.next;
        for(int i=0;i<size-1;i++){
            str=str+node.value+",";
            node=node.next;
        }
        str=str+node.value+"}";
        return str;
    }

    public void add(E e){
        LinkNode node=new LinkNode<E>();
        node.value=e;
        rear.next=node;
        rear=node;
        size++;
    }
    public E delete(){
        if(size==0){
            return null;
        }
        E e=this.front.next.value;
        this.front.next=this.front.next.next;
        size--;
        if(this.front.next==null){
            this.rear=this.front;
        }
        return e;
    }
    public int size(){
        return this.size;
    }


}
